﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SetupLauncher.Domain.Logic
{
    public class CreateParameter
    {
        Logger.ILoggable _log;
        Parameter.IParameter _parameter;

        Entity.Product _product;
        Entity.Environment _environment;
        Entity.Version _version;

        public string ParameterPath { get; private set; }
        public string ConfigPath { get; private set; }

        public CreateParameter(string parameterPath, string configPath, Entity.Product product, Entity.Environment environment, Entity.Version version)
        {
            Initialize(parameterPath, configPath, product, environment, version, null);
        }

        public CreateParameter(string parameterPath, string configPath, Entity.Product product, Entity.Environment environment, Entity.Version version, Logger.ILoggable log)
        {
            Initialize(parameterPath, configPath, product, environment, version, log);
        }

        private void Initialize(string parameterPath, string configPath, Entity.Product product, Entity.Environment environment, Entity.Version version, Logger.ILoggable log)
        {

            this._log = log;
            this._product = product;
            this._environment = environment;
            this._version = version;
            
            this.ConfigPath = configPath;
            this.ParameterPath = parameterPath;
            
            Exceptions.ValidationException exception = new Exceptions.ValidationException();

            if (string.IsNullOrEmpty(this.ConfigPath))
                exception.AddValidation(new Exceptions.Validation
                {
                    Description = "Nenhum Caminho para o arquivo de configuração foi Informado",
                    PropertyName = "configPath",
                    PropertyTitle = "Caminho para o arquivo de configuração"
                });            


            if (string.IsNullOrEmpty(_product.Name))
                exception.AddValidation(new Exceptions.Validation
                {
                    Description = "Nenhum Nome de produto foi Informado",
                    PropertyName = "productName",
                    PropertyTitle = "Nome de produto"
                });

            if (exception.CountValidations > 0)
                throw exception;


            _parameter = Parameter.ParameterFactory.Create(_product.Name, _log);
        }

        public void AlterConfiguration()
        {
            _parameter.CreateConfig(_product, _environment, this.ConfigPath); 
        }
        public void AlterParameter()
        {
            _parameter.CreateParameter(_product, _environment, _version, this.ParameterPath);
        }
    }
}
